package edu.hhu.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import edu.hhu.basic.utils.FormatUtils;

public class BaController extends Controller {
	public void index() {
		String sql1 = "select * from data_dictionary where sort is not NULL order by sort";
		List<Record> td = Db.find(sql1);
		String sql2 = "select * from (SELECT p.wellcode,p.wellname,p.depth,p.totalthickness,p.CAL,p.SP,p.GR,p.RD,p.RS,p.RXO,p.CNL,p.AC,p.DEN,p.pressure,p.PCPG,p.ACS,p.pressurelan, q.layered,q.DOT FROM (SELECT f.wellcode,f.wellname,f.depth,f.totalthickness,f.CAL,f.SP,f.GR,f.RD,f.RS,f.RXO,f.CNL,f.AC,f.DEN,f.pressure,f.PCPG,h.ACS,h.pressurelan FROM (SELECT c.wellcode,c.wellname,c.depth,c.totalthickness,c.CAL,c.SP,c.GR,c.RD,c.RS,c.RXO,c.CNL,c.AC,c.DEN,e.pressure,e.PCPG FROM (select a.wellcode,a.wellname,a.depth,a.totalthickness,b.CAL,b.SP,b.GR,b.RD,b.RS,b.RXO,b.CNL,b.AC,b.DEN FROM jtjcsj a LEFT join mccjsj b ON a.wellcode=b.wellcode) c LEFT JOIN mcsjsj e on e.wellcode=c.wellcode) f LEFT JOIN fxhysj h on f.wellcode=h.wellcode) p LEFT JOIN mcddbcjsj q on p.wellcode=q.wellcode) s WHERE s.wellcode in (SELECT wellcode FROM cjkj where wellgroupcode='延1井组');";
		render("/basic1/query.jsp");
	}

	// 单井显示
	public void query() {
		String sql1 = "select * from data_dictionary where sort is not NULL order by sort";
		List<Record> td = Db.find(sql1);
		renderJson(td);
		// renderJson(list);
	}

	public void query1() {
		String para = getPara("wellname");
		String[] split = para.split(",");
		int pagno = Integer.parseInt(getPara("page"));
		String str = "";
		for (int i = 0; i < split.length; i++) {
			str += "?";
			if (i < split.length - 1) {
				str += ",";
			}
		}
		String sql2 = "from (SELECT p.wellcode,p.wellname,p.depth,p.totalthickness,p.CAL,p.SP,p.GR,p.RD,p.RS,p.RXO,p.CNL,p.AC,p.DEN,p.pressure,p.PCPG,p.ACS,p.pressurelan, q.layered,q.DOT FROM (SELECT f.wellcode,f.wellname,f.depth,f.totalthickness,f.CAL,f.SP,f.GR,f.RD,f.RS,f.RXO,f.CNL,f.AC,f.DEN,f.pressure,f.PCPG,h.ACS,h.pressurelan FROM (SELECT c.wellcode,c.wellname,c.depth,c.totalthickness,c.CAL,c.SP,c.GR,c.RD,c.RS,c.RXO,c.CNL,c.AC,c.DEN,e.pressure,e.PCPG FROM (select a.wellcode,a.wellname,a.depth,a.totalthickness,b.CAL,b.SP,b.GR,b.RD,b.RS,b.RXO,b.CNL,b.AC,b.DEN FROM jtjcsj a LEFT join mccjsj b ON a.wellcode=b.wellcode) c LEFT JOIN mcsjsj e on e.wellcode=c.wellcode) f LEFT JOIN fxhysj h on f.wellcode=h.wellcode) p LEFT JOIN mcddbcjsj q on p.wellcode=q.wellcode) s WHERE s.wellcode in (SELECT wellcode FROM cjkj where wellname in ("
				+ str + "))";
		System.out.println(sql2);
		Page<Record> page = Db.paginate(pagno, 20, "select *", sql2, split);
		FormatUtils.DealNull(page.getList());
		renderJson(page);
		// renderJson(list);
	}

	public void column2() {
		String tablename = getPara("tablename");
		String sql2 = "SELECT * from data_dictionary where tablename=?";
		List<Record> list = Db.find(sql2, tablename);
		FormatUtils.DealNull(list);
		renderJson(list);
	}

	public void query2() {
		String para = getPara("wellname");
		String tablename = getPara("tablename");

		String sql2 = "SELECT * from" + " " + tablename + " "
				+ "where wellcode=(select wellcode from cjkj where wellname='"
				+ para + "')";
		System.out.println(sql2);
		List<Record> list = Db.find(sql2);
		FormatUtils.DealNull(list);
		if (list == null) {
			list = new ArrayList<Record>();
		}
		renderJson(list);
	}

}
